define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'handler/handler/index' + location.search,
                    add_url: 'handler/handler/add',
                    edit_url: 'handler/handler/edit',
                    del_url: 'handler/handler/del',
                    multi_url: 'handler/handler/multi',
                    import_url: 'handler/handler/import',
                    table: 'handler',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'level.name', title: __('Level.name'), operate: 'LIKE'},
                        {field: 'level.commission_rate', title: __('Level.commission_rate'), operate:'BETWEEN'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'real_name', title: __('Real_name'), visible: false, operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'total_income', title: __('Total_income'), operate:'BETWEEN'},
                        {field: 'total_orders', title: __('Total_orders')},
                        {field: 'rating', title: __('Rating'), operate:'BETWEEN'},
                        {field: 'skills', title: __('Skills'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'id_card', title: __('Id_card'), visible: false, operate: 'LIKE'},
                        {field: 'bank_card', title: __('Bank_card'), visible: false, operate: 'LIKE'},
                        {field: 'bank_name', title: __('Bank_name'), visible: false, operate: 'LIKE'},
                        {field: 'id_card_front_image', title: __('Id_card_front_image'), visible: false, operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'id_card_back_image', title: __('Id_card_back_image'), visible: false, operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'custom', title: __('切换'), operate: false, formatter: Controller.api.formatter.custom},
                        {field: 'audit_remark', title: __('Audit_remark'), visible: false, operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'description', title: __('Description'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), visible: false, operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'buttons',
                            width: "120px",
                            title: __('结算'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('结算'),
                                    title: __('结算'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'handler/handler/detail',
                                    callback: function (data) {
                                        Fast.api.ajax({
                                            url: 'handler/handler/detail/ids/'+ data.ids,
                                            data: {amount:data.amount}
                                        }, function(data, ret) {
                                            // 成功回调
                                            Layer.alert("结算【"+JSON.stringify(data.amount)+"】成功,打手当前余额" + JSON.stringify(data.newBalance), {title: "操作成功"});
                                            table.bootstrapTable('refresh');
                                        }, function(data, ret) {
                                            // 失败回调
                                            Layer.alert(ret.msg, {title: "操作失败", icon: 2});
                                        });
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'handler/handler/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'handler/handler/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'handler/handler/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $(document).on('click', '.btn-callback', function () {
                Fast.api.close({ids:$("input[name=callback-id]").val(),amount:$("input[name=callback]").val()});
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },

            formatter: {//渲染的方法
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="handler/handler/change" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.custom == 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
        }
    };
    return Controller;
});
